/**
 * Copyright (c) 2016, smuyyh@gmail.com All Rights Reserved.
 * #                                                   #
 * #                       _oo0oo_                     #
 * #                      o8888888o                    #
 * #                      88" . "88                    #
 * #                      (| -_- |)                    #
 * #                      0\  =  /0                    #
 * #                    ___/`---'\___                  #
 * #                  .' \\|     |# '.                 #
 * #                 / \\|||  :  |||# \                #
 * #                / _||||| -:- |||||- \              #
 * #               |   | \\\  -  #/ |   |              #
 * #               | \_|  ''\---/''  |_/ |             #
 * #               \  .-\__  '-'  ___/-. /             #
 * #             ___'. .'  /--.--\  `. .'___           #
 * #          ."" '<  `.___\_<|>_/___.' >' "".         #
 * #         | | :  `- \`.;`\ _ /`;.`/ - ` : | |       #
 * #         \  \ `_.   \_ __\ /__ _/   .-` /  /       #
 * #     =====`-.____`.___ \_____/___.-`___.-'=====    #
 * #                       `=---='                     #
 * #     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~   #
 * #                                                   #
 * #               佛祖保佑         永无BUG             #
 * #                                                   #
 */
package com.hzf.booklibrary.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;

import com.hzf.booklibrary.R;
import com.hzf.booklibrary.base.BaseActivity;
import com.hzf.booklibrary.base.Constant;
import com.hzf.booklibrary.component.AppComponent;
import com.hzf.booklibrary.component.DaggerMainComponent;
import com.hzf.booklibrary.okhttp.ResultCallBack;
import com.hzf.booklibrary.ui.contract.MainContract;
import com.hzf.booklibrary.ui.fragment.RecommendFragment;
import com.hzf.booklibrary.ui.presenter.MainActivityPresenter;
import com.hzf.booklibrary.utils.DeviceUtils;
import com.hzf.booklibrary.utils.NetworkUtils;
import com.hzf.booklibrary.utils.ToastUtils;

import org.json.JSONException;
import org.json.JSONObject;

import javax.inject.Inject;

import butterknife.OnClick;
import okhttp3.Request;

/**
 *  主页面
 */
public class MainActivity extends BaseActivity implements MainContract.View {

    //Presenter采用Dragger2注入依赖
    private RecommendFragment fragment;


    @Inject
    MainActivityPresenter mPresenter;

    // 退出时间
    private long currentBackPressedTime = 0;
    // 退出间隔
    private static final int BACK_PRESSED_INTERVAL = 2000;



    @Override
    public int getLayoutId() {
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        return R.layout.activity_main;

    }
    //Dragger2:注入到此Activity
    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerMainComponent.builder()
                .appComponent(appComponent)
                .build()
                .inject(this);
    }


    @Override
    public void initTitleBar() {
       mBtnRight.setText("设置");
       mBtnRight.setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(R.drawable.img_setting_right),null,null,null);
       mBtnLeft.setText("选择分类");

//        if(!DeviceUtils.isXiaoPai(this)){
//            if(mBtnLeft!= null )
//                mBtnLeft.setVisibility(View.INVISIBLE);
//        }

       mBtnLeft.setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(R.drawable.img_select_classify_left),null,null,null);
       tvCenter.setBackgroundResource(R.drawable.clazz_book);
    }
    @OnClick(R.id.btn_left)
    public void onClickLeft(){
        startActivity(new Intent(this,ClassifyActivity.class));
    }
    @OnClick(R.id.btn_right)
    public void onClickRight(){SettingActivity.startActivity(this);}

    @Override
    public void initDatas() {

        if(Constant.API_BASE_URL.equals( "")){
            NetworkUtils.getBookServerIp(this, new ResultCallBack() {
                @Override
                public void onError(Request request, Exception e) {

                }

                @Override
                public void onResponse(String response) {
                    System.out.println(response);
                    try {
                        JSONObject json = new JSONObject(response);
                        String ip = json.getString("school_clture_service_url");
                        Constant.API_BASE_URL = ip+"/";

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            });
        }

        FragmentManager fragmentManager = getSupportFragmentManager();
        fragment = new RecommendFragment();
        fragmentManager.beginTransaction().add(R.id.ll_main_container,fragment).commit();

        Bundle bundle=new Bundle();
        bundle.putString("item","推荐");
        fragment.setArguments(bundle);

    }

    @Override
    public void configViews() {
        hideStatusBar();
        mPresenter.attachView(this);
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_DOWN
                && event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
            if (System.currentTimeMillis() - currentBackPressedTime > BACK_PRESSED_INTERVAL) {
                currentBackPressedTime = System.currentTimeMillis();
                ToastUtils.showToast(getString(R.string.exit_tips));
                return true;
            } else {
                finish(); // 退出
               }
        }
        return super.dispatchKeyEvent(event);
    }



    @Override
    public void syncBookShelfCompleted() {}


    @Override
    public void showError() {}

    @Override
    public void complete() {}

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.detachView();
        }
    }
}